package com.bins.charts.service.impl;

import com.bins.charts.service.EChartService;
import com.bins.charts.vo.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class EChartServiceImpl implements EChartService {

    @Override
    public FertilizerAndGrainChartVo getFertilizerAndGrainChartData() {
        FertilizerAndGrainChartVo chartVo = new FertilizerAndGrainChartVo();

//        TitleField title = new TitleField();
//        title.setText("Relationship between chemical fertilizer and grain");
//        chartVo.setTitle(title);

        TooltipField tooltip = new TooltipField();
        tooltip.setTrigger("axis");
        chartVo.setTooltip(tooltip);

        LegendField legend = new LegendField();
        List<String> legendData = Arrays.asList("fertilizer", "grain");
        legend.setData(legendData);
        chartVo.setLegend(legend);

        GridField grid = new GridField();
        grid.setLeft("3%");
        grid.setRight("4%");
        grid.setBottom("3%");
        grid.setContainLabel(true);
        chartVo.setGrid(grid);

        ToolboxField toolbox = new ToolboxField();
        FeatureField feature = new FeatureField();
        SaveAsImageField saveAsImage = new SaveAsImageField();
        feature.setSaveAsImage(saveAsImage);
        toolbox.setFeature(feature);
        chartVo.setToolbox(toolbox);

        XAxisField xAxis = new XAxisField();
        xAxis.setType("category");
        xAxis.setBoundaryGap(false);
        List<String> xAxisData = Arrays.asList("1959", "1960", "1961", "1962", "1963", "1964", "1965");
        xAxis.setData(xAxisData);
        chartVo.setXAxis(xAxis);

        YAxisField yAxis = new YAxisField();
        yAxis.setType("value");
        chartVo.setYAxis(yAxis);

        List<SeriesField> seriesList = new ArrayList<>();
        SeriesField sf1 = new SeriesField();
        sf1.setName("fertilizer");
        sf1.setType("line");
        sf1.setStack("Total");
        List<Integer> sf1Data = Arrays.asList(120, 132, 101, 134, 90, 230, 210);
        sf1.setData(sf1Data);
        seriesList.add(sf1);

        SeriesField sf2 = new SeriesField();
        sf2.setName("grain");
        sf2.setType("line");
        sf2.setStack("Total");
        List<Integer> sf2Data = Arrays.asList(220, 182, 191, 234, 290, 330, 310);
        sf2.setData(sf1Data);
        seriesList.add(sf2);
        chartVo.setSeries(seriesList);
        return chartVo;
    }
}
